﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Common.Component;

namespace Runda.EShop.Web.Order
{
    public partial class OrderReceive : BasePage
    {
        public Guid OrderID
        {
            get
            {
                if (Request.QueryString["OrderID"] != null)
                {
                    return new Guid(Request.QueryString["OrderID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {
            //检查订单状态是否是
            BizOrderBll bll = new BizOrderBll();
            BizOrderEntity entity = bll.Get<BizOrderEntity>(OrderID);
            if (entity.OrderStatus != (int)EnumHelper.OrderStatus.DeliveryComplete)
            {
                //ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "success", "alert('订单未交付，不能确认！.');window.location='OrderList.aspx';", true);
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('订单未交付，不能确认！.');window.location='OrderList.aspx';", true);
                return;
            }
            GetOrderPatch();
        }

        private void GetOrderPatch()
        {
            if (OrderID == Guid.Empty) return;
            BizOrderDetailPatchBll bll = new BizOrderDetailPatchBll();
            DataTable dt = bll.GetOrderDetailPatch(OrderID);
            gvOrderPatch.DataSource = dt;
            gvOrderPatch.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BizOrderBll bll = new BizOrderBll();
     
            //确认总订单
            bll.UpdateOrderStatus(OrderID, EnumHelper.OrderStatus.Success);
             
            //ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "success", "alert('操作成功！.');window.location='OrderList.aspx';", true);
            ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('操作成功！.');window.location='OrderList.aspx';", true);
        }

        protected void lnkbtnPrint_Click(object sender, EventArgs e)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            parm = new SqlDbParameter();
            parm.ColumnName = "[OrderID]";
            parm.ColumnType = DbType.Guid;
            parm.ParameterName = "@OrderID";
            parm.ParameterValue = OrderID;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);


            Session["Order_Condition"] = parms;

            ClientScript.RegisterStartupScript(this.GetType(), "print", "window.open('OrderPrint.aspx')", true);
        }
    }
}